/**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2016-2026 hmc,Inc.All Rights Reserved.
 */
package com.sys.midware.mongo.core;

/**
 * mongodb获取连接管理类 <b>ClassName：</b> MongodbManager <br/>
 * <b>Description：</b> mongodb管理类 <br/>
 * <b>@author：</b> mobing <br/>
 * <b>@date：</b> 2015年10月20日 下午4:49:42 <br/>
 * <b>@version: </b> <br/>
 */
public class MongoConnection3X {
//    private static final Log logger = LogFactory.getLog(MongoConnection3X.class);
//
//    private static MongoConnection3X dbManager = new MongoConnection3X();
//
//    private static MongoClient mongoClient = null;
//
//    // private static String username = null;
//    //
//    // private static String password = null;
//    //
//    // private static boolean auth;
//    private static MongoConfig mongoConfig = null;
//
//    private MongoConnection3X() {
//
//    }
//
//    /**
//     * 描述:初始化mongoclient
//     * 
//     * @param
//     * @return void
//     */
//    private static void init(MongoConfig mc) {
//        mongoConfig = mc;
//        if (mc == null) {
//            Assert.notNull(mc, "mongoconfig is not null!");
//        }
//        // username = mc.getUserName();
//        // password = mc.getPassword();
//        // auth = mc.isAuth();
//        int maxWaitTime = mc.getMaxWaitTime();
//        int maxThreadCount = mc.getThreadsAllowedToBlockForConnectionMultiplier();
//        // connectionPool
//        try {
////            MongoOptions mongoOption = new MongoOptions();
////            mongoOption.setAutoConnectRetry(mc.isAutoConnectRetry());
////            mongoOption.setConnectionsPerHost(mc.getConnectionPoolSize());
////            mongoOption.setMaxWaitTime(maxWaitTime);
////            mongoOption.setThreadsAllowedToBlockForConnectionMultiplier(maxThreadCount);
////            ServerAddress address = new ServerAddress(mc.getHost(), mc.getPort());
////            mongoClient = new Mongo(address, mongoOption);
//            
//            ServerAddress address = new ServerAddress(mc.getHost(), mc.getPort());
//            mongoClient =   new MongoClient(address);
//        } catch (Exception e) {
//            logger.error("mongoClient 初始化异常", e);
//        }
//        addShutdownHook(mongoClient);
//    }
//
//    public static MongoConnection3X getInstance() {
//        Map<String, MongoConfig> mogoConfigMap = MongoConfigUtil.initConfig();
//
//        // 如果没有传nodename，则随机取一个mongo配置
//        for (Entry<String, MongoConfig> map : mogoConfigMap.entrySet()) {
//            mongoConfig = map.getValue();
//            if (mongoConfig == null) {
//                continue;
//            }
//        }
//        init(mongoConfig);
//        return dbManager;
//    }
//
//    public static MongoConnection3X getInstance(String nodeName) {
//        Map<String, MongoConfig> mogoConfigMap = MongoConfigUtil.initConfig();
//        MongoConfig mongoConfig = mogoConfigMap.get(nodeName);
//        init(mongoConfig);
//        return dbManager;
//    }
//
////    public DB getDB(String dbname) {
////        DB db = null;
////        try {
////            db = mongoClient.getDB(dbname);
////            if (mongoConfig.isAuth() && !db.isAuthenticated()) {
////                db.authenticate(mongoConfig.getUserName(), mongoConfig.getPassword().toCharArray());
////            }
////        } catch (Exception e) {
////            logger.error("get mongodb fail:"+dbname, e);
////        }
////        return db;
////    }
//    
//    public MongoDatabase getDB(String dbname) {
//        MongoDatabase db = null;
//        try {
//            db = mongoClient.getDatabase(dbname);
//           
////            if (mongoConfig.isAuth() && !db.isAuthenticated()) {
////                db.authenticate(mongoConfig.getUserName(), mongoConfig.getPassword().toCharArray());
////            }
//        } catch (Exception e) {
//            logger.error("get mongodb fail:"+dbname, e);
//        }
//        return db;
//    }
//
//    /**
//     * Add shutdown hook.
//     */
//    private static void addShutdownHook(final Mongo mongoClient) {
//        // create shutdown hook
//        Runnable shutdownHook = new Runnable() {
//
//            public void run() {
//                logger.info("关闭MongoDB连接池...");
//                mongoClient.close();
//            }
//        };
//        // add shutdown hook
//        Runtime.getRuntime().addShutdownHook(new Thread(shutdownHook));
//    }
//
//    public static void main(String[] args) throws MongoException, UnknownHostException {
////        DB db = MongoConnection3X.getInstance("master1").getDB("data_yintai_com");
////        System.out.println(db.getCollection("data_yintai_com"));
//
//        
//      MongoDatabase db = MongoConnection3X.getInstance("master1").getDB("data_yintai_com");
//      System.out.println(db.getName());
//    }

}
